package com.taiyi.renaissancemall.ui.main.first.memberBenefits.editOrder;

import android.content.Intent;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.FreightBean;
import com.taiyi.renaissancemall.bean.MemberEditOrderBean;
import com.taiyi.renaissancemall.bean.MemberOrderCommitBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;
import com.taiyi.renaissancemall.ui.main.first.memberBenefits.MemberBenefitsView;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class MemberEditOrderPresent extends BasePresent<MemberEditOrderView> {

    public void toEditDetail(String commodityid, String num) {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", commodityid);
        map.put("num", num);
        HttpUtils.postRequest(BaseUrl.GO_ADD_ORDER, this, map, new JsonCallback<BaseResultBean<MemberEditOrderBean>>() {
            @Override
            public BaseResultBean<MemberEditOrderBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<MemberEditOrderBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.toEditSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.toEditFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<MemberEditOrderBean>> response) {
                super.onError(response);
            }
        });
    }

    public void toBuyNow(String commodityid, String paytype, String number, String addressid, String remark) {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", commodityid);
        map.put("paytype", paytype);//支付类型1微信2支付宝3余额
        map.put("number", number);
        map.put("addressid", addressid);
        map.put("remark", remark);
        HttpUtils.postRequest(BaseUrl.ORDER_ADD_BY_NOW, this, map, new JsonCallback<BaseResultBean<MemberOrderCommitBean>>() {
            @Override
            public BaseResultBean<MemberOrderCommitBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<MemberOrderCommitBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.toBuyNowSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.toBuyNowFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<MemberOrderCommitBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getUserDetail() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.GET_PERSON_INFO, this, map, new JsonCallback<BaseResultBean<PersonInfoBean>>() {
            @Override
            public BaseResultBean<PersonInfoBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<PersonInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getUserDetailSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getUserDetailFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<PersonInfoBean>> response) {
                super.onError(response);
            }
        });
    }

    public void findOrderState(String orderid) {
        Map<String, String> map = new HashMap<>();
        map.put("orderid", orderid);
        HttpUtils.postRequest(BaseUrl.FIND_ORDER_STATE, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getOrderStateSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    findOrderState(orderid);
                    view.getOrderStateFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getFreight(String addressId) {
        Map<String, String> map = new HashMap<>();
        map.put("addressid", addressId);
        map.put("commodityspecids", "");
        HttpUtils.postRequest(BaseUrl.GET_FREIGHT, this, map, new JsonCallback<BaseResultBean<FreightBean>>() {

            @Override
            public BaseResultBean<FreightBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FreightBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getFreightSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getFreightFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FreightBean>> response) {
                super.onError(response);
            }

        });
    }

}
